// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.stream.StreamUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.fowo.api.common.model.R;
import com.fowo.api.datainterface.util.JsonUtil;
import com.fowo.api.datainterface.util.RedisUtil;
import com.fowo.api.entity.IssuOrder;
import com.fowo.api.entity.NavSellOrder;
import com.fowo.api.model.issu.order.IssuOrderVo;
import com.fowo.api.model.nav.sell.order.NavSellOrderVo;
import com.fowo.api.model.nav.sell.order.declare.NavSellOrderDeclareItemVo;
import com.fowo.api.model.nav.sell.order.declare.NavSellOrderDeclareSearchParamPo;
import com.fowo.api.model.nav.sell.order.detail.NavSellOrderDetailItemVo;
import com.fowo.api.model.nav.sell.order.detail.NavSellOrderDetailSearchParamPo;
import com.fowo.api.model.nav.sell.order.platform.NavSellOrderPlatformItemVo;
import com.fowo.api.model.nav.sell.order.platform.NavSellOrderPlatformSearchParamPo;
import com.fowo.api.model.nav.sell.order.transaction.NavSellOrderTransactionItemVo;
import com.fowo.api.model.nav.sell.order.transaction.NavSellOrderTransactionSearchParamPo;
import com.fowo.api.service.IssuOrderPlatformService;
import com.fowo.api.sys.ssf.SimpleStatusFlowService;
import com.fowo.api.user.model.JwtUserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 订单管理订单标发 服务实现类
 */
@Service
@Slf4j
@DS("ds24")
public class IssuOrderServiceImpl extends IssuOrderBaseServiceImpl {


    @Resource
    protected NavSellOrderServiceImpl navSellOrderService;

    /**
     * 标发
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markDelivery(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            List<NavSellOrderVo> list = navSellOrderService.getVoByIdList(ids, true);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("1"), a.getIssuStatus())), "校验异常,请检查单据状态!");// 1:待审核 2:待发货 3:发货中 4:已发货 5:不发货 6:平台发货 -1:已取消
            Assert.isTrue(list.stream().allMatch(a -> StrUtil.isNotBlank(a.getDeliveryIssuId())), "包含物流未完成的单据,不可标发!");
            navSellOrderService.markDelivery(list);
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }

    /**
     * 不标发
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unMarkDelivery(List<Long> ids) throws Exception {
        Assert.isTrue(CollUtil.isNotEmpty(ids), "参数异常!");
        try {
            List<NavSellOrderVo> list = navSellOrderService.getVoByIdList(ids, false);
            Assert.isTrue(CollUtil.isNotEmpty(list), "请选择对应订单!");
            Assert.isTrue(list.stream().allMatch(a -> CollUtil.safeContains(CollUtil.newArrayList("1", "2", "3", "4"), a.getIssuStatus())), "校验异常,请检查单据状态!");// 1:待审核 2:待发货 3:发货中 4:已发货 5:不发货 6:平台发货 -1:已取消
            navSellOrderService.unMarkDelivery(list);
        } catch (Exception ex) {
            log.error("发生异常,异常信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
            throw new RuntimeException("发生异常,信息:" + StrUtil.subPre(ExceptionUtil.getRootCauseMessage(ex), 2000));
        }
    }


}
